Skip to content

Fix #15081 (usability: try to report misuse of exclude options) - #8906

Open
danmar wants to merge 4 commits into
cppcheck-opensource:mainfrom
cppchecksolutions:unused-exclude
Open

danmar wants to merge 4 commits into
cppcheck-opensource:mainfrom
cppchecksolutions:unused-exclude

Conversation

@danmar

@danmar danmar commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

Copilot AI balanced review requested due to automatic review settings September 30, 2026 19:57

This comment was marked as outdated.

Copilot AI balanced review requested due to automatic review settings September 30, 2026 20:01

This comment was marked as outdated.

@danmar
danmar marked this pull request as ready for review October 1, 2026 16:30
@danmar
danmar requested a balanced review from Copilot October 1, 2026 16:30

This comment was marked as outdated.

@danmar danmar changed the title Unused exclude Fix #15081 (usability: try to report misuse of exclude options) Oct 1, 2026
@danmar
danmar requested a balanced review from Copilot October 2, 2026 04:29

This comment was marked as outdated.

Comment thread cli/cmdlineparser.cpp Resolved
Comment thread lib/pathmatch.cpp Resolved
@danmar

danmar commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator Author

@claude review

Comment thread lib/pathmatch.cpp Outdated
// paths inside a matched directory are not traversed, so a pattern that is
// covered by a matched pattern is considered used
const Filemode mode = !s.empty() && PathIterator::issep(s.back(), mSyntax) ? Filemode::directory : Filemode::regular;
return std::none_of(mMatchedPatterns.cbegin(), mMatchedPatterns.cend(), [&](const std::string& matched) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

False negative: this "covered by a matched pattern" check runs against every matched pattern, but the reasoning only holds for patterns that matched a directory. Only directory matches stop the traversal. A file pattern never hides anything.

Examples where a stale pattern goes unreported:

  • -ifoo.cpp -inonexistent/foo.cpp: foo.cpp matches lib/foo.cpp, and match("foo.cpp", "nonexistent/foo.cpp") is true, so the second pattern counts as used.
  • -i*_test.cpp -imissing_test.cpp: the glob matches some file, so missing_test.cpp is never reported.

Suggestion: keep track of which patterns matched with Filemode::directory, and only use those for this check.

Comment thread cli/cmdlineparser.cpp

const auto& unmatched = matcher.unmatched();
if (!unmatched.empty()) {
mLogger.printError("unused ignore/exclude path '" + unmatched.front() + "'. To hide warnings in certain files use suppressions instead.");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of points about making this a hard error:

  • Only unmatched.front() is reported. If a user has several stale -i paths, they have to fix one and re-run for each. Listing all of them would be easier to use (same for the project branch below).
  • -ifoo.h now always fails, because headers are never passed to match(). The user first sees the existing "filename exclusion does not apply to header files" info message and then this error. It may be worth making the two messages consistent.
  • Builds that share one ignore list across different path arguments will now fail. So will builds that ignore generated directories that don't exist yet, e.g. -ibuild/ on a fresh checkout. Is a hard error (rather than a warning) intended here? If so, the release note should call it out as a breaking change.

@claude

claude Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Overall the approach looks good: PathMatch records which patterns matched, and moving --file-filter out of importVcxproj means the filter no longer causes false "unused" reports. Points from the review:

  • False negative in PathMatch::unmatched() (inline): the "covered by a matched pattern" check should only use patterns that matched a directory. As written, an earlier match of foo.cpp or *_test.cpp hides stale patterns like nonexistent/foo.cpp or missing_test.cpp. A test such as PathMatch({"foo.cpp", "x/foo.cpp"}) that only matches lib/foo.cpp would catch this.
  • CLI (inline): only the first unused path is reported. -i<header> now always fails. Making this a hard error is a breaking change for scripts that share ignore lists or ignore directories that don't exist yet.
  • GUI inconsistency: MainWindow::doAnalyzeProject still throws away the return value of p.ignorePaths(v). So GUI projects that import a compile database, sln or vcxproj never report unused excludes, although the release note says the GUI now reports them.
  • Tests: there is no CLI/python test for an unused exclude coming from a .cppcheck project's excludedPaths, or for --file-filter with a vcxproj/sln now that filtering moved out of importVcxproj.

🤖 Generated with Claude Code

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

New false positive in PathMatch::unmatched(). An exclude pattern that matches files only inside a directory that another pattern already excludes is reported as unused, and the run stops with exit code 1. The "covered" check only compares the two pattern strings with each other (match("src/sub/", "b.c")). It never asks whether the second pattern would match something inside the skipped directory. So -ithird_party/ -i*.pb.cc . fails when all .pb.cc files are under third_party/. Ignore lists like this are common, and they worked before this PR. In my tests this was the only new problem.

Several earlier points are still in the code:

  • MainWindow::doAnalyzeProject still discards the result of ignorePaths (C2/C18).
  • The covered check still hides stale file/glob patterns (C11/C16).
  • The GUI message is still a run-on sentence (C6).
  • Only the first unused path is reported (C17).

Comments

18 comments, 6 open, 1 new
# Comment By Where Status
1 Callers/Windows definitions not updated for non-const PathMatch @Copilot cli/filelister.h:62 ✅ Fixed
2 GUI doAnalyzeProject discards unmatched excludes from ignorePaths @Copilot lib/importproject.cpp:251 ☑️ Resolved, code unchanged
3 any_of stops at first pattern so overlapping patterns are not recorded @Copilot lib/pathmatch.cpp:40 ✅ Fixed
4 CLI error message run-on sentence @Copilot cli/cmdlineparser.cpp:269 ✅ Fixed
5 GUI warning text run-on, lowercase 'to hide warnings' @Copilot gui/mainwindow.cpp:713 ☑️ Resolved, code unchanged
6 Project excludes validated against partial recheck subset @copilot-pull-request-reviewer[bot] general ✅ Fixed
7 Redundant second exclusion pass when no exclude list @Copilot gui/filelist.cpp:111 ✅ Fixed
8 Project excludes validated against recheck subset in GUI @Copilot gui/mainwindow.cpp:687 ✅ Fixed
9 Covered check uses all matched patterns, not only directory matches (stale patterns hidden) @Copilot lib/pathmatch.cpp:169 ⚠️ Open (false negative)
10 Hard error breaks existing command lines; suggest a warning @danmar cli/cmdlineparser.cpp:269 ☑️ Resolved, code unchanged
11 Header excludes can never be used and now cause an error @danmar lib/pathmatch.cpp:160 💬 Answered: Author: "its ok"
12 Covered check should only use directory matches; stale file/glob patterns not reported @claude[bot] lib/pathmatch.cpp:168 ⚠️ Open (false negative)
13 Only the first unused path is reported @claude[bot] cli/cmdlineparser.cpp:269 ⚠️ Open
14 -i always fails with duplicate header info message @claude[bot] cli/cmdlineparser.cpp:269 💬 Answered: Author said "its ok" in the C14 thread
15 Hard error is a breaking change for shared ignore lists/nonexistent dirs @claude[bot] cli/cmdlineparser.cpp:269 ⚠️ Open
16 GUI doAnalyzeProject still ignores result of ignorePaths @claude[bot] general ⚠️ Open (false negative)
17 No CLI/python tests for .cppcheck excludedPaths or --file-filter with vcxproj/sln @claude[bot] general ⚠️ Open
18 Patterns that match files inside an excluded directory are reported as unused AI review lib/pathmatch.cpp:168 🆕 New

@danmar, please fix, resolve or reply to the open and new comments: #9, #12, #13, #15, #16, #17, #18.
For #9, #12, #16 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.

Testing

Builds

  • make -j8 cppcheck testrunner in pr/ built without errors.
  • I also built the GUI and its tests with CMake.

Unit and GUI tests

  • ./testrunner TestPathMatch TestCmdlineParser TestFileLister TestImportProject: 466 tests, 0 failed.
  • GUI ctest (test-filelist and the others): 5/5 passed.

Python tests in test/cli

  • 585 passed and 16 failed.
  • 12 of the failures are rule tests, because this build has no rules support ("Option --rule ...").
  • The other 4 are test_redundant_file_reads, which fails with FileNotFoundError, most likely a tool missing in the container.
  • I don't think any of these failures are related to the PR, but I did not run them against base/.

Manual CLI tests (in /work/scratch/t1)

  • These pass: -isrc/sub/ src, -isub src, -i./src/sub/ src, absolute -i, -isrc/sub/ src/sub/b.c src/a.c, and -isrc/sub/ -i**/sub/ src.

Reproducer for the false positive

mkdir -p src/sub third_party/proto
echo "int a;" > src/a.c; echo "int b;" > src/sub/b.c; echo "int p;" > third_party/proto/x.pb.cc

$ cppcheck -q -isrc/sub/ -ib.c src
cppcheck: error: unused ignore/exclude path 'b.c'. To hide warnings in certain files use suppressions instead.
rc=1
$ cppcheck -q -isrc/sub/ -isub/b.c src
cppcheck: error: unused ignore/exclude path 'sub/b.c'. To hide warnings in certain files use suppressions instead.
rc=1
$ cppcheck -q -ithird_party/ -i*.pb.cc .
cppcheck: error: unused ignore/exclude path '*.pb.cc'. To hide warnings in certain files use suppressions instead.
rc=1

In each case the reported pattern does match an existing file (src/sub/b.c, third_party/proto/x.pb.cc). It is reported only because the directory is never traversed. Without this PR these command lines run normally.


Automated review of 0d886c7 by Claude. The pull request was built and tested locally.

Comment thread lib/pathmatch.cpp Outdated
// paths inside a matched directory are not traversed, so a pattern that is
// covered by a matched pattern is considered used
const Filemode mode = !s.empty() && PathIterator::issep(s.back(), mSyntax) ? Filemode::directory : Filemode::regular;
return std::none_of(mMatchedPatterns.cbegin(), mMatchedPatterns.cend(), [&](const std::string& matched) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The covered check only tests whether a matched pattern matches the text of the unmatched pattern. When a directory is excluded, files below it are never passed to match(). So a pattern that would only match files in there is reported as unused even though it does match existing files, unless its text happens to start with the directory's path. Examples that now fail with exit code 1:

$ cppcheck -isrc/sub/ -ib.c src          # src/sub/b.c exists
cppcheck: error: unused ignore/exclude path 'b.c'. ...
$ cppcheck -isrc/sub/ -isub/b.c src
cppcheck: error: unused ignore/exclude path 'sub/b.c'. ...
$ cppcheck -ithird_party/ -i*.pb.cc .    # all .pb.cc are below third_party/
cppcheck: error: unused ignore/exclude path '*.pb.cc'. ...

Ignore lists like -ithird_party/ -i*.pb.cc are common, and they worked before this PR. A comparison of pattern strings can't decide whether one pattern is "covered" by another. Two possible fixes:

  • Keep walking the excluded directories (or at least record the paths below them) when you collect which patterns are used.
  • Report an unmatched pattern only when it could not match anything in an excluded directory.

@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit 0d886c7 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8906/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: -0.4% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8906/2026-11-18_14-58-48-0d886c725116_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

dmcppcheck and others added 2 commits November 19, 2026 19:10
Add the scenario "Unused exclude paths are reported" with the test
project unused-exclude: one exclude path that matches a file and one that
matches nothing. Only the unused path is reported.

Regenerate the script of "Other Help menu entries" against a build that
has online-help.qhc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants